package tests;

import static org.junit.Assert.*;
import hexgame.GameLogic;
import hexgame.GameState;
import hexgame.HexMove;
import hexgame.HexPlayer;

import org.junit.Test;

import ai.minimax.MiniMaxAI;

public class MiniMaxTests {

	@Test
	public void testBasicBoard() {
		GameState state = new GameState(6, true);
		
		assertEquals(6, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		HexMove move = new HexMove(0, 5, HexPlayer.RED_LOCAL);
		assertTrue(GameLogic.isValidMove(state, move));
		GameLogic.makeMove(state, move);
		
		assertEquals(5, MiniMaxAI.minimumMovesUntilTarget(state, true));
	}
	
	@Test
	public void testCompleteGame() {
		GameState state = new GameState(6, true);
		assertEquals(6, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		HexMove move = new HexMove(0, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		move = new HexMove(5, 0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		assertEquals(5, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		move = new HexMove(1, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		move = new HexMove(4, 0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		assertEquals(4, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		move = new HexMove(2, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		move = new HexMove(3, 0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		//assertEquals(3, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		move = new HexMove(3, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		move = new HexMove(2, 0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		//assertEquals(2, MiniMaxAI.minimumMovesUntilTarget(state, true));
		
		move = new HexMove(4, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		move = new HexMove(1, 0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		//assertEquals(1, MiniMaxAI.minimumMovesUntilTarget(state, true));

		move = new HexMove(5, 5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(HexPlayer.RED_LOCAL, GameLogic.playerWon(state));
		assertEquals(0, MiniMaxAI.minimumMovesUntilTarget(state, true));
	}
	
	@Test
	public void testReverseCompleteGame() {
		GameState state = new GameState(6, false);
		assertEquals(6, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		HexMove move = new HexMove(5,0, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		move = new HexMove(0,5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(5, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		move = new HexMove(5,1, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		move = new HexMove(1,5, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(4, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		move = new HexMove(4,1, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		move = new HexMove(0,3, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(4, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		move = new HexMove(5,2, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		move = new HexMove(0,2, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(3, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		move = new HexMove(5,3, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		move = new HexMove(1,2, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(2, MiniMaxAI.minimumMovesUntilTarget(state, false));
		
		move = new HexMove(5,4, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		move = new HexMove(0,1, HexPlayer.RED_LOCAL);
		GameLogic.makeMove(state, move);
		
		assertEquals(1, MiniMaxAI.minimumMovesUntilTarget(state, false));

		move = new HexMove(5, 5, HexPlayer.BLUE_REMOTE);
		GameLogic.makeMove(state, move);
		
		assertEquals(HexPlayer.BLUE_REMOTE, GameLogic.playerWon(state));
		assertEquals(0, MiniMaxAI.minimumMovesUntilTarget(state, false));
	}
}
